package com.xhwl.centiotdata.suppliersystem.huawei;

import com.alibaba.fastjson.JSONObject;
import com.huaweicloud.sdk.core.exception.ClientRequestException;
import com.xhwl.centiotdata.pojo.constant.HuaWeiIoTDAConstant;
import com.xhwl.centiotdata.pojo.dto.huawei.IOTConfigDTO;
import com.xhwl.common.dto.cent.iotdata.light.LightActionDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yanyixiong
 * @Description:
 * @Date: create in 10:01 2022/1/12
 */
@Service
@Slf4j
public class LightHuaWeiIoTDAHandler {

    @Autowired
    private GeneralHuaWeiIoTDAHandler generalHuaWeiIoTDAHandler;

    public boolean switchLight(IOTConfigDTO configDTO, LightActionDTO lightActionDTO){
        boolean isSuccess = true;
        Map<String, Short> params = new HashMap<>(1);
        try{
            params.put("DO",lightActionDTO.getSwitchValue());
            JSONObject jsonObject = generalHuaWeiIoTDAHandler.command(configDTO.getAk(), configDTO.getSk(), configDTO.getRegion(), lightActionDTO.getLoopCode(), HuaWeiIoTDAConstant.LIGHT_SERVICE_ID, "DO", params);
            if(jsonObject == null || jsonObject.getInteger("result_code")!=1){
                isSuccess = false;
            }
        }catch (ClientRequestException e){
            log.error("回路执行异常：{}",e.getErrorMsg());
            isSuccess = false;
        }
        return isSuccess;
    }

}
